home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / musik / MIDIFP21 / SOURCES / MIDI_PRT / PRINTER / PRINTER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  11.3 KB  |  372 lines

  1. /**************************************************************
  2. *
  3. *                PRINTER.C
  4. *
  5. **************************************************************/
  6.  
  7. #include <acs.h>
  8. #include <acsplus.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <tos.h>
  12. #include <messages.pif>
  13. #include <acs_plus.pif>
  14. #include <diskfile.pif>
  15. #include "..\drawer\drawer.pif"
  16. #include "..\params\params.pif"
  17.  
  18. #include "printer.pif"
  19. #include "printer.h"
  20. #include "printer.ah"
  21.  
  22.  
  23. /************* OBJECT DATA PROTOTYPE BEGIN ***********************/
  24.  
  25. typedef struct 
  26. {
  27.     /*** init only data ***/
  28.     char path[pathname_length], filename[filename_length] ;
  29.     int print_handle, print_width, print_height ;
  30.     int screen_handle, screen_width, screen_height ;
  31.     Awindow *drawer_wi ;
  32. PRINTER ;
  33.  
  34. /************* OBJECT DATA PROTOTYPE END *************************/
  35.  
  36.  
  37.  
  38. /*******************************************************************
  39. */
  40.     static int service(Awindow *wi, int task, void *not_used)
  41. /*
  42. * Service routine for messages sent to the printer window.
  43. * OUTPUT:  AS_TERM ==> message is forwarded to parent
  44. *          AS_INFO ==> a little help function 
  45. *******************************************************************/
  46. {
  47.     switch (task)
  48.     {    
  49.         case AS_TERM: Awi_sendall(TERMINATE, wi) ; return TRUE ; 
  50.         case AS_INFO: A_dialog(&PRINTER_INFO) ;    return TRUE ;
  51.         default:      return FALSE ;
  52.     }
  53. }
  54.  
  55.  
  56. static void to_file(void)
  57.     ev_object[INTO_ONE].ob_state ^= DISABLED ;
  58.     Awi_obchange(ev_window, INTO_ONE, -1) ; 
  59. }
  60.  
  61. /*******************************************************************
  62. */
  63.     static void go(void)
  64. /*
  65. * for the "go" button in the printer
  66. * window work object. A number of pages corresponding to the user's
  67. * entries in the work object is sent to printer or 1 page is sent to
  68. * metafile driver. Selection between printer and metafile driver is 
  69. * done depending on the "to file" checkbox. 
  70. * The metafile driver is loaded when output to file is selected.
  71. * Same is valid for metafile fonts. 
  72. * The metafile is written to the path of the GEM clipboard. If no 
  73. * path is set the GEM clipboard is set to "current drive":\clipbrd .
  74. * If that directory does not exist it is created.
  75. * The printer driver is expected to be already loaded !
  76. * Same is valid for printer fonts.
  77. * OUTPUT:    - a directory \clipbrd if no clipboard path is set
  78. *              and output to file is desired
  79. *            - one or several pages with note systems generated 
  80. *              via VDI routines on the printer or 
  81. *              the metafile workstation 
  82. *******************************************************************/
  83. {
  84. PRINTER *p = ev_window->user ;
  85. int first, last, page, i, length ;
  86. char path_buffer[pathname_length], clipboard[pathname_length], metafilename[filename_length] ;
  87. char *point, pagestring[9] ;
  88. int points[4], drive, drive_buffer ;
  89. unsigned long existing_drives ;
  90. int handle, work_in[11], work_out[57] ;
  91.  
  92.     /*** enable page number GEM object ***/
  93.     Awi_obchange(ev_window, PAGE, ev_object[PAGE].ob_state & ~DISABLED) ;      
  94.  
  95.     /*** determine first and last page to be drawn ***/
  96.     if (ev_object[FROM_TO].ob_state & SELECTED)
  97.     {
  98.         first = atoi(ev_object[FROM].ob_spec.tedinfo->te_ptext) ;
  99.         page = get_first_page(p->drawer_wi) ;
  100.         if (first < page) first = page ;
  101.         last = atoi(ev_object[TO].ob_spec.tedinfo->te_ptext) ;
  102.         page = get_last_page(p->drawer_wi) ;
  103.         if (last > page) last = page ;
  104.     }
  105.     if (ev_object[CURRENT].ob_state & SELECTED)
  106.         first = last = get_current_page(p->drawer_wi) ;
  107.     if (ev_object[ALL].ob_state & SELECTED)
  108.     {
  109.         first = get_first_page(p->drawer_wi) ;
  110.         last = get_last_page(p->drawer_wi) ;
  111.     }
  112.     /*** print (to printer or to file ?) ***/
  113.     if (ev_object[TO_FILE].ob_state & SELECTED)
  114.     {
  115.         /*** buffer current drive and path ***/
  116.         drive_buffer = Dgetdrv() ;
  117.         Dgetpath(path_buffer, 0) ;
  118.  
  119.         /*** check which drives exist (get bit table) ***/
  120.         existing_drives = Dsetdrv(drive_buffer) ;
  121.  
  122.         /*** get/set clipboard path ***/
  123.         scrp_read(clipboard) ;
  124.         if (clipboard[0])
  125.         {
  126.             /*** test if clipboard path already defined physically exists ***/
  127.             Dsetdrv( (*clipboard - 'A') & 0x1F ) ;
  128.             if ( Dsetpath(clipboard + 2) < 0 )
  129.             {
  130.                 alert_str(PATH_NOT_EXIST, clipboard) ;
  131.                 clipboard[0] = 0 ;
  132.             }
  133.         }
  134.         else
  135.         {
  136.             /*** no valid clipboard path defined ***/
  137.             existing_drives >>= 2 ; /* drive 'C' in least bit */
  138.  
  139.             /*** search first existing drive ***/
  140.             for ( drive = 'C' ; drive <= 'Z' ; drive++ )
  141.             {
  142.                 if (existing_drives & 0x0001)
  143.                 {
  144.                     clipboard[0] = drive ; 
  145.                     drive = 'Z' + 2 ;
  146.                 }
  147.                 existing_drives >>= 1 ; /* test next drive */
  148.             }
  149.             /*** if no drive found, use current drive ***/
  150.             if ( !clipboard[0] ) clipboard[0] = drive_buffer + 'A' ;
  151.             clipboard[1] = 0 ;
  152.             strcat(clipboard, ":\\CLIPBRD\\") ;
  153.  
  154.             /*** test if clipboard path already exists ***/
  155.             Dsetdrv( (*clipboard - 'A') & 0x1F ) ;
  156.             if ( Dsetpath("\\CLIPBRD\\") < 0 )
  157.             {
  158.                 /*** directory must be created ***/
  159.                 Dsetpath("\\") ;
  160.                 if ( Dcreate("CLIPBRD") < 0 )
  161.                 {
  162.                     alert_str(DIR_NOT_CREATED, clipboard) ;
  163.                     Dsetpath(p->path) ;
  164.                 }
  165.                 else
  166.                 {
  167.                     /*** define path for clipboard ***/
  168.                     alert_str(DIR_CREATED, clipboard) ;
  169.                     Dsetpath("\\CLIPBRD\\") ;
  170.                     scrp_write(clipboard) ;
  171.                 }
  172.             }
  173.         }
  174.         if (ev_object[INTO_ONE].ob_state & SELECTED)
  175.         {
  176.             /*** all pages into SCRAP.GEM separated by form advances ***/
  177.             /*** open physical workstation (metafile driver) ***/
  178.             work_in[0] = 31 ; /* metafile driver of assign.sys */
  179.             for ( i = 1 ; i <= 8 ; i++ ) work_in[i] = 1 ; 
  180.             work_in[9] = 0 ; work_in[10] = RC ; 
  181.             v_opnwk(work_in, &handle, work_out) ;
  182.             if (handle)
  183.             {
  184.                 vm_filename(handle, "SCRAP.GEM") ;
  185.  
  186.                 /*** load metafile fonts ***/
  187.                 vst_load_fonts(handle, 0) ;
  188.  
  189.                 /*** layout  and "print" ***/
  190.                 if ( page_layouter(p->screen_handle, p->screen_width, p->screen_height, 
  191.                      p->drawer_wi) == LAYOUT_GOOD ) for ( page = first ; page <= last ; page++ ) 
  192.                 {
  193.                     itoa(page, ev_object[PAGE].ob_spec.tedinfo->te_ptext, 10) ;
  194.                     Awi_obchange(ev_window, PAGE_CARRIER, -1) ;      
  195.                     points[0] = points[1] = 0 ;
  196.                     points[2] = p->screen_width ; 
  197.                     points[3] = p->screen_height ;
  198.                     draw_page(handle, p->drawer_wi, 0, 0, points, 1, page) ;
  199.                     v_form_adv(handle) ;
  200.                 }
  201.                 reset_layout(p->drawer_wi) ;
  202.                 /*** never change the sequence of the following functions: PURE C error !!! ***/
  203.                 v_meta_extents(handle, 0, 0, p->screen_width, p->screen_height) ;
  204.                 vm_pagesize(handle, 2090, 2970) ;    /* DIN A4 */
  205.                 vm_coords(handle, 0, p->screen_height, p->screen_width, 0) ;
  206.  
  207.                 vst_unload_fonts(handle, 0) ;    
  208.                 v_clswk(handle) ;
  209.             }
  210.             else alert_str(METAFILING_NOT_POSSIBLE, "") ;
  211.         }
  212.         else for ( page = first ; page <= last ; page++ )
  213.         {
  214.             /*** 1 file per page ***/ 
  215.             itoa(page, ev_object[PAGE].ob_spec.tedinfo->te_ptext, 10) ;
  216.             Awi_obchange(ev_window, PAGE_CARRIER, -1) ;      
  217.  
  218.             /*** open physical workstation (metafile driver) ***/
  219.             work_in[0] = 31 ; /* metafile driver of assign.sys */
  220.             for ( i = 1 ; i <= 8 ; i++ ) work_in[i] = 1 ; 
  221.             work_in[9] = 0 ; work_in[10] = RC ; 
  222.             v_opnwk(work_in, &handle, work_out) ;
  223.             if (handle)
  224.             {
  225.                 /*** set file name, set extension to "GEM", include page number ***/
  226.                 strcpy(metafilename, p->filename) ;
  227.                 point = strrchr(metafilename, '.') ;
  228.                 if (!point) 
  229.                 {
  230.                     point = metafilename + strlen(metafilename) ;
  231.                     point[0] = '.' ; point[1] = 0 ;
  232.                 }
  233.                 strcpy( point + 1, "GEM" ) ;
  234.                 itoa(page, pagestring, 10) ;
  235.                 length = strlen(pagestring) ;
  236.                 memcpy( point - length, pagestring, length ) ;
  237.                 vm_filename(handle, metafilename) ;
  238.  
  239.                 /*** load metafile fonts ***/
  240.                 vst_load_fonts(handle, 0) ;
  241.  
  242.                 /*** layout  and "print" ***/
  243.                 if ( page_layouter(p->screen_handle, p->screen_width, p->screen_height, 
  244.                      p->drawer_wi) == LAYOUT_GOOD )   
  245.                 {
  246.                     points[0] = points[1] = 0 ;
  247.                     points[2] = p->screen_width ; 
  248.                     points[3] = p->screen_height ;
  249.                     draw_page(handle, p->drawer_wi, 0, 0, points, 1, page) ;
  250.                     v_meta_extents(handle, 0, 0, p->screen_width, p->screen_height) ;
  251.                     vm_pagesize(handle, 2090, 2970) ;
  252.                     vm_coords(handle, 0, p->screen_height, p->screen_width, 0) ;
  253.                 }
  254.                 reset_layout(p->drawer_wi) ;
  255.                 vst_unload_fonts(handle, 0) ;    
  256.                 v_clswk(handle) ;
  257.             }
  258.             else alert_str(METAFILING_NOT_POSSIBLE, "") ;
  259.         }
  260.  
  261.         /*** "GEMFILE.GEM" exists because of GDOS error ? ***/
  262.         Fdelete("GEMFILE.GEM") ; /* a workaround ! */
  263.  
  264.         /*** restore original drive and path ***/        
  265.         Dsetdrv(drive_buffer) ;
  266.         Dsetpath(path_buffer) ;
  267.     }
  268.     else 
  269.     {
  270.         /*** generate printed paper ***/
  271.         if ( page_layouter(p->print_handle, 
  272.              p->print_width, p->print_height, p->drawer_wi) != LAYOUT_GOOD )
  273.         {        
  274.             alert_str(NO_LAYOUT, "") ;
  275.             reset_layout(p->drawer_wi) ;
  276.             return ;
  277.         }    
  278.         points[0] = points[1] = 0 ;
  279.         points[2] = p->print_width - 1 ; 
  280.         points[3] = p->print_height - 1 ;
  281.         for ( page = first ; page <= last ; page++ )
  282.         {
  283.             itoa(page, ev_object[PAGE].ob_spec.tedinfo->te_ptext, 10) ;
  284.             Awi_obchange(ev_window, PAGE, -1) ;      
  285.             draw_page(p->print_handle, p->drawer_wi, 0, 0, points, 1, page) ;
  286.             v_updwk(p->print_handle) ;
  287.             v_clrwk(p->print_handle) ;
  288.         }
  289.         reset_layout(p->drawer_wi) ;
  290.     }
  291.     /*** disable page number GEM object ***/
  292.     Awi_obchange(ev_window, PAGE, ev_object[PAGE].ob_state | DISABLED) ;      
  293. }
  294.  
  295.  
  296. /*******************************************************************
  297. *
  298. *             PUBLISHED INTERFACE
  299. *
  300. *******************************************************************/
  301.  
  302.  
  303. void PRINTER_destructor(Awindow *wi)
  304. {
  305.     if (wi) 
  306.     {
  307.         memory_symptom = "PrDstr wi->user" ;
  308.         if (wi->user) My_free(wi->user) ;
  309.         memory_symptom = "PrDstr wi" ;
  310.         Awi_delete(wi) ;
  311.     }
  312. }
  313.  
  314.  
  315. Awindow *PRINTER_constructor(PRINTER_INIT *printer_init)
  316. {
  317. Awindow *wi ;
  318. PRINTER *p ;
  319.  
  320.     /*** enter filename into icon text and info line ***/
  321.     PRINTER_WINDOW.iconblk = printer_init->icon ;
  322.     PRINTER_WINDOW.iconblk->monoblk.ib_ptext = printer_init->filename ;
  323.     PRINTER_WINDOW.info = printer_init->filename ;
  324.  
  325.     /*** create the PRINTER window ***/
  326.     wi = Awi_create(&PRINTER_WINDOW) ;
  327.     if (!wi) return NULL ;
  328.  
  329.     /*** allocate printer structure and initialize ***/
  330.     wi->user = p = My_alloc(sizeof *p) ;
  331.     if (!p) { PRINTER_destructor(wi) ; return NULL ; }
  332.     strcpy(p->filename, printer_init->filename) ;
  333.     strcpy(p->path, printer_init->path) ;
  334.     strrchr(p->path, '\\')[1] = 0 ;
  335.     p->screen_handle = printer_init->screen_handle ;
  336.     p->screen_width =  printer_init->screen_width ;
  337.     p->screen_height = printer_init->screen_height ;
  338.     p->print_handle = printer_init->print_handle ;
  339.     p->print_width =  printer_init->print_width ;
  340.     p->print_height = printer_init->print_height ;
  341.     p->drawer_wi = printer_init->drawer_window ;
  342.  
  343.     /*** disable buttons if necessary ***/
  344.     if (!p->print_handle) 
  345.     {
  346.         /*** printing not possible ***/
  347.         if (application)
  348.             /*** only metafiling possible ***/
  349.             wi->work[TO_FILE].ob_state |= (SELECTED | DISABLED) ;
  350.         else
  351.             /*** nothing possible in accessory ***/
  352.             wi->work[GO].ob_state |= DISABLED ;
  353.     }
  354.     else
  355.     {
  356.         /*** printing possible ***/
  357.         if (!application)
  358.             /*** metafiling not possible in accessory ***/
  359.             wi->work[TO_FILE].ob_state |= DISABLED ;
  360.     }
  361.     /*** return success message ***/
  362.     return wi ;
  363. }
  364.  
  365.  
  366.